<template>
    <div>

    </div>
</template>
<script>
    import swal from "sweetalert";

    export default {
        data() {
            return {

            }
        },
        created() {
            let code = this.getQueryString("code");
            if (code) {
                let promote_code = localStorage.getItem("promote_code");
                this.api.join(promote_code, code).then(res => {
                    localStorage.removeItem("promote_code");
                    if (res.meta.code == '-1' && res.data.openid) {
                        localStorage.setItem("wx_openid", res.data.openid);
                        swal(res.meta.msg).then((value) => {
                            window.location.replace('/center');
                        });
                    } else if (res.meta.code == '000' && res.data.openid) {
                        localStorage.setItem("wx_openid", res.data.openid);
                        window.location.replace('/center');
                    } else {
                        window.location.href = '/';
                    }
                })
            } else {
                window.location.href = '/';
                // this.$router.replace("/");
            }



            // let appid = this.globalData.WX_APP_ID;
            // let url = this.globalData.HOTS + localStorage.getItem("now_url");
            // let code = this.getUrlParame('code'); // 截取code
            // if (!code) {
            //     window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(url)}&response_type=code&scope=snsapi_userinfo#wechat_redirect`
            // } else {
            //     let promote_code = localStorage.getItem("promote_code");
            //     this.api.join(promote_code, code).then(res => {
            //         console.log(res);
            //         let promote_code = localStorage.removeItem("promote_code");
            //         if (res.meta.code == '-1' && res.data.openid) {
            //             localStorage.setItem("wx_openid", res.data.openid);
            //             alert(res.meta.msg);
            //             window.location.replace('/center');
            //         } else if (res.meta.code == '000' && res.data.openid) {
            //             localStorage.setItem("wx_openid", res.data.openid);
            //             window.location.replace('/center');
            //         } else {
            //             window.location.href = '/';
            //         }
            //     })
            // }
        },
        mounted() {
            // this.postAcceptInvite();
        },
        methods: {
            // 获取地址栏参数
            getQueryString(n) {
                let r, reg = new RegExp("(^|&)" + n + "=([^&]*)(&|$)");
                r = window.location.search.substr(1).match(reg);
                return r ? decodeURI(r[2]) : null;
            },
            // 截取code
            getUrlParame(parameName) {
                /// 获取地址栏指定参数的值
                /// <param name="parameName">参数名</param>
                // 获取url中跟在问号后面的部分
                var parames = window.location.search;
                // 检测参数是否存在
                if (parames.indexOf(parameName) > -1) {
                    var parameValue = '';
                    parameValue = parames.substring(parames.indexOf(parameName), parames.length)
                    // 检测后面是否还有参数
                    if (parameValue.indexOf('&') > -1) {
                        // 去除后面多余的参数, 得到最终 parameName=parameValue 形式的值
                        parameValue = parameValue.substring(0, parameValue.indexOf('&'))
                        // 去掉参数名, 得到最终纯值字符串
                        parameValue = parameValue.replace(parameName + '=', '')
                        return parameValue;
                    }
                }
                return '';
            }


        },
    }
</script>
<style scoped>


</style>
